<template>
  <div class="app-container">
    <!-- 查询模块 -->
    <div class="formDiv">
      <el-form ref="form"  :inline="true" label-width="140px">
        <el-row>
          <el-col :span="6">
            <el-form-item label="角色名称">
              <el-input v-model="form.roleName"></el-input>
            </el-form-item>
          </el-col>

          <el-col :span="6">
            <el-form-item label="所属系统">
              <Platform @childfun="childfun"></Platform>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <!-- <el-form-item> -->
            <el-button style="margin-left:60px" type="primary" @click="searchFn">查询</el-button>
            <el-button
              v-if="hasPermission('AdminRoleProvider.addAdminRole')"
              type="primary"
              @click="toRoutes('roleManageAdd')"
            >新增</el-button>
            <!-- </el-form-item> -->
          </el-col>
        </el-row>
      </el-form>
    </div>
    <!-- 主页列表 -->
    <div class="listDiv" v-loading="tableLoading">
      <!-- table列表 -->
      <el-table :data="tableData" class="tableData" style="width: 100%">
        <el-table-column label="部门" width="120">
          <template slot-scope="scope" v-if="scope.row">{{scope.row.deptName}}</template>
        </el-table-column>
        <el-table-column prop="roleName" label="角色名称" width="120"></el-table-column>
        <el-table-column label="权限">
          <template slot-scope="scope">
            <span v-for="(i,key) in scope.row.permissions " :key="key">
              <span>{{i.name}}</span>
              <span>&emsp;</span>
            </span>
          </template>
        </el-table-column>
        <el-table-column prop="createdDate" label="创建时间" width="100"></el-table-column>
        <el-table-column label="状态" width="80">
          <template slot-scope="scope">
            <el-switch
              class="switchStyle"
              v-model="scope.row.status"
              @click.native="changeStatus(scope.row.id,'status',scope.row.status,scope.row)"
              active-color="#13ce66"
              inactive-color="#eee"
              :active-value="0"
              :inactive-value="1"
            ></el-switch>
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="120">
          <template slot-scope="scope">
            <el-button
              v-if="hasPermission('AdminRoleProvider.updateAdminRole')"
              type="text"
              @click="toRoutes('roleManageEdit',scope.row.id)"
              size="small"
            >编辑</el-button>
            <el-button
              v-if="hasPermission('AdminRoleProvider.deleteAdminRole')"
              type="text"
              v-show="!scope.row.status"
              @click="deleteFn(scope.row.id)"
              size="small"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <div class="pagesCon">
        <div>
          <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="listForm.pageNum"
            :page-sizes="page_sizes"
            :page-size="listForm.limit"
            layout="total, sizes, prev, pager, next, jumper"
            :total="page_totals"
          ></el-pagination>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import {
  listAdminRoles,
  deleteAdminRole,
  updateAdminRole
} from "@/api/roleManage";
import Platform from "../components/platform";

export default {
  name: "roleManageIndex",
  components: { Platform },
  data() {
    return {
      dialogVisible: true,
      tableLoading: true,
      date: "",
      value1: "",
      options: [
        {
          value: "0",
          label: "正常"
        },
        {
          value: "1",
          label: "停用"
        }
      ],
      form: {
        roleName: "",
        pageNum: 1,
        pageSize: 10
      },
      tableData: [],
      page_sizes: [10, 30, 50, 100],
      page_totals: 60,
      listForm: {
        pageNum: 1,
        pageSize: 10
      }
    };
  },
  mounted() {
    this.getList(this.listForm);
  },
  methods: {
    //搜索的
    childfun(val) {
      console.log(typeof val);
      this.form.subSys = val ===''?-1:val;
    },
    //按钮权限
    hasPermission(e) {
      return window.helper.hasPermission(e);
    },
    //成功状态弹窗
    succStatus(txt) {
      this.$message({
        message: txt + "成功",
        type: "success"
      });
    },
    //失败状态弹窗
    errorStatus(txt) {
      this.$message.error(txt + "失败");
    },
    //查询
    searchFn() {
      this.form.pageNum = 1;
      console.log(this.form);
       this.getList(this.form);
    },
    // 路由跳转
    toRoutes(e, userId) {
      let route = { name: e, query: { userId: userId } };
      this.$router.push(route);
    },
    //列表数据获取
    getList(e) {
      this.tableLoading = true;
      let datas = e;
      listAdminRoles(datas)
        .then(res => {
          console.log(res);
          if (!res.data.code) {
            // console.log(res)
            this.tableData = res.data.data.roleResponseDTOS;
            this.page_totals = res.data.data.count;
          } else {
            this.$message.error(res.data.msg);
          }
          this.tableLoading = false;
        })
        .catch(error => {
          this.$message.error(error);
        });
    },
    //删除
    deleteFn(e) {
      let datas = {
        id: Number(e)
      };
      this.$confirm("删除该条数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          deleteAdminRole(datas)
            .then(res => {
              console.log(res);
              if (!res.data.code) {
                this.succStatus("删除");
                this.getList(this.form);
              } else {
                this.$message.error(res.data.msg);
              }
            })
            .catch(error => {
              this.$message.error(error);
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    //改变页面条数
    handleSizeChange(val) {
      this.form.pageSize = val;
      this.getList(this.form);
    },
    //选择页数
    handleCurrentChange(val) {
      this.form.pageNum = val;
      this.getList(this.form);
    },
    //上下架与状态修改
    changeStatus(id, type, status, row) {
      let datas = {
        id: id,
        deptId: row.deptId,
        roleName: row.roleName
      };
      datas[type] = status;
      updateAdminRole(datas).then(res => {
        // console.log(res)
        if (res.data.code === 0) {
          this.$message({
            message: "修改成功",
            type: "success"
          });
          this.getList(this.form);
        } else {
          this.$message.error("修改失败");
        }
      });
      console.log(datas);
    }
  }
};
</script>
<style scoped>
/* 分页 */
.pagesCon {
  height: 40px;
  margin-top: 20px;
}
.pagesCon > div {
  float: right;
}
</style>
